package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallFeedback;
import org.linlinjava.litemall.db.mapper.LitemallFeedbackDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author Yogeek
 * @date 2018/8/27 11:39
 */
@Service
public class LitemallFeedbackService {
    @Autowired
    private LitemallFeedbackDao feedbackDao;

    public Integer add(LitemallFeedback feedback) {
        feedback.setAddTime(LocalDateTime.now());
        feedback.setUpdateTime(LocalDateTime.now());
        feedbackDao.insertTemplate(feedback,true);
        return 1;
       // return feedbackMapper.insertSelective(feedback);
    }

    public List<LitemallFeedback> querySelective(Integer userId, String username, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallFeedback> query = feedbackDao.createLambdaQuery();
    	if (userId != null) {
    		query.andEq(LitemallFeedback::getUserId, userId);
        }
        if (!StringUtils.isEmpty(username)) {
        	query.andLike(LitemallFeedback::getUsername, "%" + username + "%");
        }
        query.andEq(LitemallFeedback::getDeleted, false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        
        query.limit(page, limit);
        
        return query.select();
    	
		/*
		 * LitemallFeedbackExample example = new LitemallFeedbackExample();
		 * LitemallFeedbackExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (userId != null) { criteria.andUserIdEqualTo(userId); } if
		 * (!StringUtils.isEmpty(username)) { criteria.andUsernameLike("%" + username +
		 * "%"); } criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * feedbackMapper.selectByExample(example);
		 */
    }
}
